package com.rex.saas.service;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rex.saas.bean.DeviceBasicData;
import com.rex.saas.bean.PropertyCondition;
import com.rex.saas.bean.request.AddStrategyTaskRequest;
import com.rex.saas.bean.request.TaskRecordPageRequest;
import com.rex.saas.constants.ApiConstant;
import com.rex.saas.constants.Constants;
import com.rex.saas.db.entity.SaasStrategyTask;
import com.rex.saas.db.entity.SaasStrategyTaskRecord;
import com.rex.saas.db.entity.SaasStrategyTaskRecordDetails;
import com.rex.saas.db.mapper.SaasStrategyTaskMapper;
import com.rex.saas.db.mapper.SaasStrategyTaskRecordDetailsMapper;
import com.rex.saas.db.mapper.SaasStrategyTaskRecordMapper;
import com.rex.saas.enums.StatusEnum;
import com.rex.saas.enums.StrategyRecordDetailsStatusEnum;
import com.rex.saas.enums.StrategyRecordStatusEnum;
import com.rex.saas.enums.StrategyTypeEnum;
import com.rex.saas.utils.CronValidator;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@Component
@Slf4j
public class StrategyTaskService {
    @Getter
    private Map<String, List<PropertyCondition>> triggerStrategyTasks = new ConcurrentHashMap<>();

    @Getter
    private Map<String, String> deviceMap = new HashMap<>();

    @Getter
    private static List<StrategyRuleType> airConRules = new ArrayList<>();

    @Getter
    private static List<StrategyRuleType> lightRules = new ArrayList<>();

    @Autowired
    SaasStrategyTaskMapper saasStrategyTaskMapper;

    @Autowired
    SaasStrategyTaskRecordMapper saasStrategyTaskRecordMapper;

    @Autowired
    SaasStrategyTaskRecordDetailsMapper saasStrategyTaskRecordDetailsMapper;

    @Autowired
    RexolarApiClient rexolarApiClient;

    static {
        airConRules.add(new StrategyRuleType("AIR_CON_ON", 1, "空调开机", "定时开启空调，并指定工作模式、温度、风速等"));
        airConRules.add(new StrategyRuleType("AIR_CON_OFF", 1, "空调关机", "定时关闭空调，可指定执行范围。 "));
        airConRules.add(new StrategyRuleType("AIR_CON_HUM", 3, "人存联动关空调", "人体存在检测到持续无人超 N 分钟，则自动执行关闭空调动作。"));

        lightRules.add(new StrategyRuleType("LIGHT_ON", 1, "灯开机", "定时开启灯"));
        lightRules.add(new StrategyRuleType("LIGHT_OFF", 1, "灯关机", "定时关闭灯"));
        lightRules.add(new StrategyRuleType("LIGHT_HUM", 3, "人存联动关灯", "人体存在检测到持续无人超 N 分钟，则自动执行关闭灯动作。"));
    }

    public List<StrategyRuleType> getAiConRuleType() {
        return airConRules;
    }

    public List<StrategyRuleType> getLightRuleType() {
        return lightRules;
    }

    public SaasStrategyTaskRecord getRecordById(Long recordId) {
        return saasStrategyTaskRecordMapper.selectById(recordId);
    }


    @AllArgsConstructor
    static class StrategyRuleType{
        private String code;
        //     * 1：一键执行场景
        //     * 2： 定时场景
        //     * 3：联动场景
        private Integer type;
        private String name;
        private String desc;
    }

    public Page<SaasStrategyTaskRecord> queryTaskRecordPage(TaskRecordPageRequest taskRecordPageRequest) {
        Page<SaasStrategyTaskRecord> page = new Page<>(taskRecordPageRequest.getPageNo(), taskRecordPageRequest.getPageSize());
        LambdaQueryWrapper<SaasStrategyTaskRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasStrategyTaskRecord::getStrategyTaskId, taskRecordPageRequest.getTaskId());
        queryWrapper.orderByDesc(SaasStrategyTaskRecord::getGmtCreated);
        return saasStrategyTaskRecordMapper.selectPage(page, queryWrapper);
    }

    public List<SaasStrategyTaskRecordDetails> queryTaskRecordDetails(Long recordId) {
        LambdaQueryWrapper<SaasStrategyTaskRecordDetails> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasStrategyTaskRecordDetails::getRecordId, recordId);
        return saasStrategyTaskRecordDetailsMapper.selectList(queryWrapper);
    }

    /**
     * 清理过期的缓存数据
     */
    public synchronized void refeshTriggerStrategyTask() {
        LambdaQueryWrapper<SaasStrategyTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SaasStrategyTask::getType, StrategyTypeEnum.LINK.getCode());
        queryWrapper.eq(SaasStrategyTask::getStatus, StatusEnum.ENABLE.getCode());
        List<SaasStrategyTask> list = saasStrategyTaskMapper.selectList(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            Map <String, List<PropertyCondition>> map = new ConcurrentHashMap<>();
            for (SaasStrategyTask saasStrategyTask : list){
                String conditions = saasStrategyTask.getConditions();
                JSONObject jsonObject = JSONObject.parseObject(conditions);
                String params = jsonObject.getString("params");
                JSONObject paramsObj = JSONObject.parseObject(params);
                String devAlias = paramsObj.getString("devAlias");
                List<PropertyCondition> conditionsList = map.get(devAlias);
                if (conditionsList == null){
                    conditionsList = new ArrayList<>();
                }
                PropertyCondition condition = new PropertyCondition();
                condition.setTaskId(saasStrategyTask.getId());
                condition.setDevAlias(devAlias);
                condition.setCompareType(paramsObj.getString("compareType"));
                condition.setPropertyName(paramsObj.getString("propertyName"));
                condition.setCompareValue(paramsObj.getString("compareValue"));
                conditionsList.add(condition);
                map.put(devAlias, conditionsList);
            }
            triggerStrategyTasks = map;
        }
    }

    public List<SaasStrategyTaskRecord> getTodoStrategyTasksRecord() {
        Date now = new Date();
        String today = DateUtil.format(now, Constants.FORMAT_DAY);
        LambdaQueryWrapper<SaasStrategyTaskRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SaasStrategyTaskRecord::getRecordDate, today);
        queryWrapper.lt(SaasStrategyTaskRecord::getScheduleTime, now);
        queryWrapper.eq(SaasStrategyTaskRecord::getStatus, StrategyRecordStatusEnum.Created.getCode());
        return saasStrategyTaskRecordMapper.selectList(queryWrapper);
    }

    public List<SaasStrategyTask> getTimeStrategyTasks() {
        LambdaQueryWrapper<SaasStrategyTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SaasStrategyTask::getType, StrategyTypeEnum.TIMER.getCode());
        queryWrapper.eq(SaasStrategyTask::getStatus, StatusEnum.ENABLE.getCode());
        return saasStrategyTaskMapper.selectList(queryWrapper);
    }

    public Long addRecord(Date time, String tenantId, Long taskId, String actions) {
        SaasStrategyTaskRecord record = new SaasStrategyTaskRecord();
        record.setTenantId(tenantId);
        record.setActions(actions);
        record.setScheduleTime(time);
        record.setStatus(StrategyRecordStatusEnum.Created.getCode());
        record.setRecordDate(DateUtil.format(time, Constants.FORMAT_DAY));
        record.setGmtCreated(time);
        record.setGmtModified(time);
        record.setStrategyTaskId(taskId);
        if(saasStrategyTaskRecordMapper.insert(record)!=1){
            log.error("addRecord fail, taskId:{}, time:{}", taskId, time);
            return null;
        }
        return record.getId();
    }

    public boolean addRecordDetails(SaasStrategyTaskRecord task) {
        SaasStrategyTaskRecordDetails details = new SaasStrategyTaskRecordDetails();
        details.setRecordId(task.getId());
        String actions = task.getActions();
        JSONObject jsonObject = JSONObject.parseObject(actions);
        details.setAction(actions);
        String deviceName = jsonObject.getString("devAlias");
        details.setDeviceName(deviceName);
        details.setStatus(StrategyRecordStatusEnum.Created.getCode());
        details.setGmtCreated(new Date());
        details.setGmtModified(new Date());
        return saasStrategyTaskRecordDetailsMapper.insert(details)==1;
    }


    public boolean executeTaskRecord(SaasStrategyTaskRecord taskRecord) {
        JSONObject jsonObject = JSONObject.parseObject(taskRecord.getActions());
        JSONArray deviceNames = jsonObject.getJSONArray("devAlias");
        for(Object deviceNameObj : deviceNames){
            String deviceName = (String) deviceNameObj;
            String serviceName = jsonObject.getString("serviceName");
            String params = jsonObject.getString("params");
            JSONObject cmd = new JSONObject();
            try {
                JSONObject tmp = JSONObject.parseObject(params);
                tmp.keySet().forEach(key -> {
                    cmd.put("Write", key);
                    cmd.put("Value", tmp.get(key));
                });
                cmd.put("DeviceId", deviceName);
                cmd.put("GroupId", "0");
            }catch (Exception e){
                log.error("executeTaskRecord ILLEGAL_PARAMETERS_JSON_ERROR, params:{}", params, e);
                return false;
            }
            String tenantId = taskRecord.getTenantId();
            String token = rexolarApiClient.getTenantToken(tenantId);
            if(StringUtils.isEmpty(token)){
                log.error("executeTaskRecord getToken error");
                return false;
            }
            DeviceBasicData deviceBasicData = rexolarApiClient.getDeviceByDeviceName(deviceName, token);
            if(deviceBasicData==null){
                log.error("executeTaskRecord getDeviceByDeviceName error, deviceName:{}", deviceName);
                continue;
            }
            String deviceId = deviceBasicData.getCoreId();
            List<DeviceBasicData> gwList = rexolarApiClient.getFromRelationsInfo(token, deviceId);
            if(CollectionUtils.isEmpty(gwList)){
                log.error("executeTaskRecord getRelationsInfo gwList is empty, deviceName:{}", deviceName);
                continue;
            }

            HashMap<String, Object> dataMap = new HashMap<>();
            dataMap.put(Constants.Version, "1.0");
            dataMap.put("VendorCode", "Rexense");
            String time = String.valueOf(System.currentTimeMillis());
            dataMap.put("Timestamp", time);
            String seq = deviceName+"-"+time;
            dataMap.put("Seq", seq);
            dataMap.put("DeviceId", gwList.get(0).getName());
            dataMap.put("FunctionCode", serviceName);
            dataMap.put("Payload", cmd);

            boolean result = rexolarApiClient.updateDeviceAttributes(gwList.get(0).getCoreId(), ApiConstant.SHARED_SCOPE, JSON.toJSONString(dataMap), token);
            if(!result){
                log.error("executeTaskRecord updateDeviceAttributes error, deviceName:{}", deviceName);
                return false;
            }else{
                SaasStrategyTaskRecordDetails details = new SaasStrategyTaskRecordDetails();
                details.setTenantId(taskRecord.getTenantId());
                details.setRecordId(taskRecord.getId());
                details.setDeviceName(deviceName);
                details.setSeq(seq);
                details.setAction(String.valueOf(jsonObject));
                details.setStatus(StrategyRecordDetailsStatusEnum.Created.getCode());
                details.setGmtCreated(new Date());
                details.setGmtModified(new Date());
                try{
                    if(saasStrategyTaskRecordDetailsMapper.insert(details)!=1){
                        log.error("executeTaskRecord insert error, details:{}", details);
                        return false;
                    }
                }catch (Exception e){
                    log.error("executeTaskRecord saasStrategyTaskRecordDetailsMapper.insert error, params:{}", params, e);
                    return false;
                }
            }
        }
        taskRecord.setStatus(StrategyRecordStatusEnum.PUSHED.getCode());
        taskRecord.setGmtModified(new Date());
        if(saasStrategyTaskRecordMapper.updateById(taskRecord)!=1){
            log.error("executeTaskRecord updateById error, taskRecord:{}", taskRecord);
            return false;
        }
        return true;
    }

    public void ackTaskRecord(JSONObject triggerRequest) {
        log.info("ackTaskRecord triggerRequest:{}", triggerRequest);
        String ackCode = triggerRequest.getString("ackCode");
        String ackSeq = triggerRequest.getString("ackSeq");
        LambdaQueryWrapper<SaasStrategyTaskRecordDetails> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasStrategyTaskRecordDetails::getSeq, ackSeq);
        List<SaasStrategyTaskRecordDetails> details = saasStrategyTaskRecordDetailsMapper.selectList(queryWrapper);
        if(CollectionUtils.isEmpty(details)){
            log.warn("ackTaskRecord ackSeq not found, ackSeq:{}", ackSeq);
            return;
        }
        details.forEach(detail->{
            if("0".equals(ackCode)){
                detail.setStatus(StrategyRecordDetailsStatusEnum.SUCCESS.getCode());
            }else if("1".equals(ackCode)){
                detail.setStatus(StrategyRecordDetailsStatusEnum.FAILED.getCode());
            }else {
                log.warn("ackTaskRecord ackCode error, ackCode:{}", ackCode);
                return;
            }
            detail.setAckTime(new Date());
            detail.setGmtModified(new Date());
            if(saasStrategyTaskRecordDetailsMapper.updateById(detail)!=1){
                log.error("ackTaskRecord updateById error, detail:{}", detail);
                return;
            }
            LambdaQueryWrapper<SaasStrategyTaskRecordDetails> allQueryWrapper = new LambdaQueryWrapper<>();
            allQueryWrapper.eq(SaasStrategyTaskRecordDetails::getRecordId, detail.getRecordId());
            List<SaasStrategyTaskRecordDetails> allDetails = saasStrategyTaskRecordDetailsMapper.selectList(allQueryWrapper);
            boolean flag = true;
            for (SaasStrategyTaskRecordDetails allDetail : allDetails){
                if (allDetail.getStatus().equals(StrategyRecordDetailsStatusEnum.Created.getCode())) {
                    flag = false;
                    break;
                }
            }
            SaasStrategyTaskRecord taskRecord = saasStrategyTaskRecordMapper.selectById(detail.getRecordId());
            if(!flag){
                taskRecord.setStatus(StrategyRecordStatusEnum.PART_SUCCESS.getCode());
            }else {
                taskRecord.setStatus(StrategyRecordStatusEnum.ALL_SUCCESS.getCode());
            }
            taskRecord.setGmtModified(new Date());
            if(saasStrategyTaskRecordMapper.updateById(taskRecord)!=1){
                log.error("ackTaskRecord updateById error, taskRecord:{}", taskRecord);
            }
        });
    }

    public List<SaasStrategyTaskRecord> queryActionTaskRecord(Long taskId, Date startTime) {
        LambdaQueryWrapper<SaasStrategyTaskRecord> allQueryWrapper = new LambdaQueryWrapper<>();
        allQueryWrapper.eq(SaasStrategyTaskRecord::getStrategyTaskId, taskId);
        allQueryWrapper.eq(SaasStrategyTaskRecord::getScheduleTime, startTime);
        return saasStrategyTaskRecordMapper.selectList(allQueryWrapper);
    }

    public List<SaasStrategyTaskRecord> queryRecoverTaskRecord(Long taskId, Date startTime, Date endTime) {
        LambdaQueryWrapper<SaasStrategyTaskRecord> allQueryWrapper = new LambdaQueryWrapper<>();
        allQueryWrapper.eq(SaasStrategyTaskRecord::getStrategyTaskId, taskId);
        if(endTime.before(startTime)){
            allQueryWrapper.le(SaasStrategyTaskRecord::getScheduleTime, endTime);
        }else{
            allQueryWrapper.eq(SaasStrategyTaskRecord::getScheduleTime, endTime);
        }
        return saasStrategyTaskRecordMapper.selectList(allQueryWrapper);
    }


    public boolean addStrategyTask(AddStrategyTaskRequest strategyTaskRequest, String tenantId) {
        SaasStrategyTask saasStrategyTask = new SaasStrategyTask();
        saasStrategyTask.setSpaceInfoId(strategyTaskRequest.getSpaceId());
        saasStrategyTask.setTenantId(tenantId);
        saasStrategyTask.setName(strategyTaskRequest.getName());
        saasStrategyTask.setStatus(StatusEnum.ENABLE.getCode());
        saasStrategyTask.setType(strategyTaskRequest.getType());
        saasStrategyTask.setConditions(JSONObject.toJSONString(strategyTaskRequest.getCondition()));
        saasStrategyTask.setActions(JSONObject.toJSONString(strategyTaskRequest.getAction()));
        saasStrategyTask.setRecovers(JSONObject.toJSONString(strategyTaskRequest.getRecovers()));
        saasStrategyTask.setGmtCreated(new Date());
        saasStrategyTask.setGmtModified(new Date());
        return saasStrategyTaskMapper.insert(saasStrategyTask)==1;
    }

    public boolean stopStrategyTask(SaasStrategyTask saasStrategyTask) {
        LambdaQueryWrapper<SaasStrategyTaskRecord> allQueryWrapper = new LambdaQueryWrapper<>();
        allQueryWrapper.eq(SaasStrategyTaskRecord::getStrategyTaskId, saasStrategyTask.getTenantId());
        allQueryWrapper.eq(SaasStrategyTaskRecord::getStatus, StrategyRecordStatusEnum.Created.getCode());
        SaasStrategyTaskRecord record = new SaasStrategyTaskRecord();
        record.setStatus(StrategyRecordStatusEnum.CANCELED.getCode());
        record.setGmtModified(new Date());
        int cnt = saasStrategyTaskRecordMapper.update(record, allQueryWrapper);
        log.info("stopStrategyTask update CANCELED  SaasStrategyTaskRecord, cnt:{}", cnt);

        saasStrategyTask.setStatus(StatusEnum.DISABLE.getCode());
        saasStrategyTask.setGmtModified(new Date());
        return saasStrategyTaskMapper.updateById(saasStrategyTask)==1;
    }

    public boolean startStrategyTask(SaasStrategyTask saasStrategyTask) {
        if(saasStrategyTask.getType().equals(StrategyTypeEnum.TIMER.getCode())){
            String conditions = saasStrategyTask.getConditions();
            JSONObject conditionsObj = JSONObject.parseObject(conditions);
            String cron = conditionsObj.getString("cron");
            if(StringUtils.isEmpty(cron)){
                log.error("startStrategyTask Timer任务cron表达式为空, task:{}",saasStrategyTask);
                return false;
            }
            Date date = new Date();
            Date startTime = CronValidator.getNextValidTime(cron, date);
            Date endTime = CronValidator.getNextInvalidTimeAfter(cron, date);
            addRecord(startTime, saasStrategyTask.getTenantId(), saasStrategyTask.getId(), saasStrategyTask.getActions());
            if(StringUtils.isEmpty(saasStrategyTask.getRecovers())){
                addRecord(endTime, saasStrategyTask.getTenantId(), saasStrategyTask.getId(), saasStrategyTask.getRecovers());
            }
        }

        saasStrategyTask.setStatus(StatusEnum.ENABLE.getCode());
        saasStrategyTask.setGmtModified(new Date());
        if(saasStrategyTaskMapper.updateById(saasStrategyTask)!=1){
            log.error("startStrategyTask updateById error, saasStrategyTask:{}", saasStrategyTask);
            return false;
        }
        return true;
    }

    public boolean deleteStrategyTask(Long taskId) {
        LambdaQueryWrapper<SaasStrategyTaskRecord> allQueryWrapper = new LambdaQueryWrapper<>();
        allQueryWrapper.eq(SaasStrategyTaskRecord::getStrategyTaskId, taskId);
        allQueryWrapper.eq(SaasStrategyTaskRecord::getStatus, StrategyRecordStatusEnum.Created.getCode());
        SaasStrategyTaskRecord record = new SaasStrategyTaskRecord();
        record.setStatus(StrategyRecordStatusEnum.CANCELED.getCode());
        record.setGmtModified(new Date());
        int cnt = saasStrategyTaskRecordMapper.update(record, allQueryWrapper);
        log.info("deleteStrategyTask update CANCELED  SaasStrategyTaskRecord, cnt:{}", cnt);

        SaasStrategyTask saasStrategyTask = new SaasStrategyTask();
        saasStrategyTask.setId(taskId);
        saasStrategyTask.setStatus(StatusEnum.DELETED.getCode());
        saasStrategyTask.setGmtModified(new Date());
        return saasStrategyTaskMapper.updateById(saasStrategyTask)==1;
    }

    public SaasStrategyTask queryById(Long id) {
        return saasStrategyTaskMapper.selectById(id);
    }

    public List<SaasStrategyTask> queryByName(String name, String tenantId, Long spaceId) {
        LambdaQueryWrapper<SaasStrategyTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasStrategyTask::getName, name);
        queryWrapper.eq(SaasStrategyTask::getTenantId, tenantId);
        queryWrapper.eq(SaasStrategyTask::getSpaceId, spaceId);
        queryWrapper.ne(SaasStrategyTask::getStatus, StatusEnum.DELETED.getCode());
        return saasStrategyTaskMapper.selectList(queryWrapper);
    }

    public boolean executeTask(Long taskId) {
        SaasStrategyTask saasStrategyTask = queryById(taskId);
        if(saasStrategyTask==null){
            log.error("executeTask error, taskId:{}", taskId);
            return false;
        }
        Long recordId = addRecord(new Date(), saasStrategyTask.getTenantId(), saasStrategyTask.getId(), saasStrategyTask.getActions());
        if(recordId==null){
            log.error("executeTask addRecord error, taskId:{}", taskId);
            return false;
        }
        SaasStrategyTaskRecord saasStrategyTaskRecord = saasStrategyTaskRecordMapper.selectById(recordId);
        boolean executeFlag = executeTaskRecord(saasStrategyTaskRecord);
        if(!executeFlag){
            log.error("executeTask executeTaskRecord error, saasStrategyTaskRecord:{}", saasStrategyTaskRecord);
            return false;
        }
        return true;
    }

    public boolean executeStrategyTask(SaasStrategyTask data) {
        Date now = new Date();
        Long recordId = addRecord(now, data.getTenantId(), data.getId(), data.getActions());
        if(recordId==null){
            log.error("executeStrategyTask addRecord error, data:{}", data);
            return false;
        }

        SaasStrategyTaskRecord saasStrategyTaskRecord = saasStrategyTaskRecordMapper.selectById(recordId);
        boolean executeFlag = executeTaskRecord(saasStrategyTaskRecord);
        if(!executeFlag){
            log.error("executeStrategyTask executeTaskRecord error, data:{}", data);
            return false;
        }
        return true;
    }

    public List<SaasStrategyTask> list(Long spaceId, String typeCode) {
        LambdaQueryWrapper<SaasStrategyTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasStrategyTask::getSpaceId, spaceId);
        queryWrapper.eq(SaasStrategyTask::getTypeCode, typeCode);
        queryWrapper.ne(SaasStrategyTask::getStatus, StatusEnum.DELETED.getCode());
        return  saasStrategyTaskMapper.selectList(queryWrapper);
    }

    public List<SaasStrategyTask> listBySpaceId(String spaceId, String tenantId) {
        LambdaQueryWrapper<SaasStrategyTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasStrategyTask::getSpaceId, spaceId);
        queryWrapper.eq(SaasStrategyTask::getTenantId, tenantId);
        queryWrapper.ne(SaasStrategyTask::getStatus, StatusEnum.DELETED.getCode());
        return  saasStrategyTaskMapper.selectList(queryWrapper);
    }

    public List<SaasStrategyTask> listBySpaceInfoId(Long spaceInfoId, String tenantId) {
        LambdaQueryWrapper<SaasStrategyTask> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasStrategyTask::getSpaceId, spaceInfoId);
        queryWrapper.eq(SaasStrategyTask::getTenantId, tenantId);
        queryWrapper.ne(SaasStrategyTask::getStatus, StatusEnum.DELETED.getCode());
        return  saasStrategyTaskMapper.selectList(queryWrapper);
    }
}
